package com.slkj.shilixiaoyuanapp.ui.teacher.tool.StudentAttendance;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;

import com.google.gson.Gson;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.GradeEntity;
import com.slkj.shilixiaoyuanapp.entity.StudentAttendanceEntity;
import com.slkj.shilixiaoyuanapp.entity.StudentAttendanceListEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.activity_student_attendance, title = "学生考勤", rightHint = "历史记录")
public class StudentAttendanceActivity extends BaseActivity {
    AttendanceListAdaper adaper;
    @BindView(R.id.recycer)
    RecyclerView recycer;
    @BindView(R.id.tv_up)
    CustomStateText tvUp;
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    List<StudentAttendanceEntity.StudentEntity> data = new ArrayList<>();
    int classId = -1;
    @Override
    protected void init() {
        ((SimpleItemAnimator)recycer.getItemAnimator()).setSupportsChangeAnimations(false);
        adaper = new AttendanceListAdaper(data);
        recycer.setLayoutManager(new LinearLayoutManager(this));
        recycer.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.HORIZONTAL));
        recycer.setAdapter(adaper);
        stateLayout.setOnReLoadListener(this::getData);
        getData();

    }

    private void getData() {
        HttpHeper.get().toolService()
                .getAllStudent()
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<StudentAttendanceEntity>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(StudentAttendanceEntity back) {
                        if (back.getLeave()!= null){
                            data.addAll(back.getLeave());
                        }if (back.getNotLeave()!=null){
                            data.addAll(back.getNotLeave());
                        }
                        classId = back.getClassId();
                        adaper.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void onRightTitleClick() {
        if (classId != -1)
            StudentAttendanceHistoryActivity__JumpCenter.builder(this).setClassId(classId).create().go();
    }
    @OnClick(R.id.tv_up)void up(){
        String json = new Gson().toJson(data);
        HttpHeper.get().toolService()
                .addCheckingin(classId,json)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String back) {
                        LoadSuccessAndFailDialog.showSuccess(StudentAttendanceActivity.this,back);
                    }
                });
    }
}
